package cc.rengu.igas.bomp.core.service.trans;

import cc.rengu.igas.bomp.common.constant.DubboMethodConstant;
import cc.rengu.igas.bomp.common.dao.WechatMchntInfoMapper;
import cc.rengu.igas.bomp.common.dao.impl.WechatMchntInfoMapperImpl;
import cc.rengu.igas.bomp.common.entity.WechatMchntInfo;
import cc.rengu.igas.bomp.common.enums.RespCodeEnum;
import cc.rengu.igas.bomp.core.service.base.ManageService;
import cc.rengu.igas.bomp.facade.bean.ViolationInfoBean;
import cc.rengu.igas.bomp.facade.request.WechatRiskMchntQueryRequest;
import cc.rengu.igas.bomp.facade.response.WechatRiskMchntQueryResponse;
import cc.rengu.igas.channel.wechat.facade.base.Header;
import cc.rengu.igas.channel.wechat.facade.bean.ViolationInfo;
import cc.rengu.igas.channel.wechat.facade.request.GetViolationRequest;
import cc.rengu.igas.channel.wechat.facade.response.GetViolationResponse;
import cc.rengu.igas.channel.wechat.facade.result.Result;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.enums.ChannelEnum;
import cc.rengu.oltp.service.model.BizResponse;
import cc.rengu.oltp.service.realize.DubboService;
import cc.rengu.oltp.service.realize.impl.DubboServiceImpl;
import cc.rengu.oltp.utility.util.BeanUtil;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.DateUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhangxuran
 * @version 1.0
 * @date 2020/4/16 15:10
 * @Description: 微信风险商户违规查询
 * <p>
 * 处理流程与曾子龙确认
 * 无特殊业务处理
 * 透传前台的数据到微信通道调用 银行特约商户违规信息查询 接口。
 * 查询微信通道返回的微信商户号查询微信商户信息表获取商户号，组装数据给前台。
 */
public class WechatRiskMchntQueryService extends ManageService {

    @Override
    protected Object bizBeforeCust() throws Exception {
        WechatRiskMchntQueryRequest wechatRiskMchntQueryRequest = new WechatRiskMchntQueryRequest();
        ConvertUtil.convertOutput(wechatRiskMchntQueryRequest);
        return wechatRiskMchntQueryRequest;
    }

    @Override
    protected BizResponse bizProcessCust(Object request) throws Exception {
        WechatRiskMchntQueryRequest wechatRiskMchntQueryRequest = (WechatRiskMchntQueryRequest) request;
        BizResponse<WechatRiskMchntQueryResponse> bizResponse = new BizResponse<>();
        WechatRiskMchntQueryResponse wechatRiskMchntQueryResponse = new WechatRiskMchntQueryResponse();

        /*获取法人行信息*/
        String instId = wechatRiskMchntQueryRequest.getHeader().getInstId();
        /*获取用户id*/
        String userId = wechatRiskMchntQueryRequest.getUserId();
        /*获取分页信息*/
        int pageIndex = wechatRiskMchntQueryRequest.getPageIndex();
        int pageSize = wechatRiskMchntQueryRequest.getPageSize();
        if (pageIndex < 1 || pageSize < 1) {
            pageIndex = 1;
            pageSize = 10;
        }
        String startDate = DateUtil.dateStringFormatConvert(wechatRiskMchntQueryRequest.getStartDate(), "yyyyMMdd", "yyyy-MM-dd");
        String endDate = DateUtil.dateStringFormatConvert(wechatRiskMchntQueryRequest.getEndDate(), "yyyyMMdd", "yyyy-MM-dd");

        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String sysSeqNum = xmlTreeUtil.getXmlTreeStringValue(TreeNodeConstant.SYS_SEQ_NUM);

        GetViolationRequest getViolationRequest = new GetViolationRequest();
        Header queryHeader = new Header();
        BeanUtil.beanCopy(wechatRiskMchntQueryRequest.getHeader(), queryHeader);
        queryHeader.setChanlId(ChannelEnum.BOMP.getChannelType());
        queryHeader.setSrcSysId(AppParamConstant.SYS_ID);
        queryHeader.setTxnNum("getviolation");
        queryHeader.setTraceNo(sysSeqNum + "1");
        getViolationRequest.setChannelId(ChannelEnum.WXZF.getChannelType());
        getViolationRequest.setChannelType(wechatRiskMchntQueryRequest.getChannelType());
        getViolationRequest.setBeginDate(startDate);
        getViolationRequest.setEndDate(endDate);
        getViolationRequest.setPageIndex(String.valueOf(wechatRiskMchntQueryRequest.getPageIndex()));
        getViolationRequest.setPageSize(String.valueOf(wechatRiskMchntQueryRequest.getPageSize()));
        getViolationRequest.setHeader(queryHeader);

        DubboService dubboService = new DubboServiceImpl();
        Result<GetViolationResponse> result = (Result<GetViolationResponse>) dubboService.callDubboService(DubboMethodConstant.DUBBO_WECHAT_MCHNT_SERVICE, "getViolation", getViolationRequest);
        if (null == result || !result.isSuccess()) {
            rglog.error("微信银行特约商户违规信息查询处理失败！调用微信通道的银行特约商户违规信息查询getViolation返回失败");
            bizResponse.setRespDesc(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
            bizResponse.setRespCode(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
            return bizResponse;
        } else if (!result.isSuccess() || !DubboMethodConstant.DUBBO_SUCCESS.equals(result.getResult().getRespCode())) {
            rglog.error("微信银行特约商户违规信息查询处理失败！调用微信通道的银行特约商户违规信息查询getViolation返回失败,errorCode:{},errorMsg:{}",
                    result.getResult().getRespCode(), result.getResult().getRespDesc());
            bizResponse.setRespDesc(result.getResult().getRespDesc());
            bizResponse.setRespCode(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode());
            return bizResponse;
        }

        List<ViolationInfo> violationInfoList = result.getResult().getViolationInfoList();
        if (CollectionUtils.isEmpty(violationInfoList)) {
            rglog.error("微信银行特约商户违规信息查询结果为空!");
            bizResponse.setRspSysId(AppParamConstant.SYS_ID);
            bizResponse.setRespCode(RespCodeEnum.QUERY_DATA_IS_NULL.getRespCode());
            bizResponse.setRespDesc(RespCodeEnum.QUERY_DATA_IS_NULL.getRespDesc());
            bizResponse.setResult(wechatRiskMchntQueryResponse);
            return bizResponse;
        } else {
            /* 根据分页查询条件截取相关数据记录 */
            int totalRows = Integer.parseInt(result.getResult().getTotalSize());//过滤前总记录数

            List<ViolationInfoBean> violationInfoBeanList = violationInfoList.stream().map(item -> convertToViolationInfoBean(item, instId)).collect(Collectors.toList());

            /* 查询结果赋值 */
            wechatRiskMchntQueryResponse.setTotalRows(totalRows);//总记录数
            wechatRiskMchntQueryResponse.setTotalPage(totalRows / pageSize);//总页数
            wechatRiskMchntQueryResponse.setPageIndex(pageIndex);
            wechatRiskMchntQueryResponse.setPageSize(pageSize);
            wechatRiskMchntQueryResponse.setViolationInfoList(violationInfoBeanList);

        }
        bizResponse.setRspSysId(AppParamConstant.SYS_ID);
        bizResponse.setRespCode(RespCodeEnum.TRANS_SUCCESS.getRespCode());
        bizResponse.setRespDesc(RespCodeEnum.TRANS_SUCCESS.getRespDesc());
        bizResponse.setResult(wechatRiskMchntQueryResponse);
        return bizResponse;
    }

    @Override
    protected void bizAfterCust(Object response) throws Exception {
        BizResponse bizResponse = (BizResponse) response;
        WechatRiskMchntQueryResponse wechatRiskMchntQueryResponse = (WechatRiskMchntQueryResponse) bizResponse.getResult();
        ConvertUtil.convertInput(wechatRiskMchntQueryResponse);
    }

    private ViolationInfoBean convertToViolationInfoBean(ViolationInfo violationInfo, String instId) {
        ViolationInfoBean violationInfoBean = new ViolationInfoBean();
        BeanUtil.beanCopy(violationInfo, violationInfoBean);
        violationInfoBean.setWechatMchntNo(violationInfo.getSubMchntNo());
        WechatMchntInfoMapper wechatMchntInfoMapper = new WechatMchntInfoMapperImpl();
        WechatMchntInfo wechatMchntInfo;
        try {
            wechatMchntInfo = wechatMchntInfoMapper.selectWechatMchntInfoByWechatMchntNo(instId, violationInfo.getSubMchntNo());
        } catch (Exception e) {
            rglog.error("通过微信号查询商户号失败！wechatNo:{}", violationInfo.getSubMchntNo());
            return violationInfoBean;
        }
        if (null != wechatMchntInfo) {
            violationInfoBean.setMchntNo(wechatMchntInfo.getMchntNo());
        }
        return violationInfoBean;
    }

}
